package com.atguigu.tingshu.order.strategy.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.order.strategy.TradeStrategy;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Package: com.atguigu.tingshu.order.strategy.impl
 *
 * @author xiongkunYang
 * @description: 策略接口具体实现类。订单项目类型为Vip
 * @since 2025/6/26 11:13
 */
@Component(SystemConstant.ORDER_ITEM_TYPE_VIP)
@Slf4j
public class VipTradeStrategy implements TradeStrategy {

    private final UserFeignClient userFeignClient;

    public VipTradeStrategy(UserFeignClient userFeignClient) {
        this.userFeignClient = userFeignClient;
    }

    @Override
    public OrderInfoVo processTrade(Long userId, TradeVo tradeVo) {
        log.info("订单项目类型为 Vip, 处理 Vip 订单...");
        OrderInfoVo orderInfoVo = new OrderInfoVo();
        // 远程调用获取 vip 服务配置详情
        Result<VipServiceConfig> vipServiceConfigResult = userFeignClient.getVipServiceConfig(tradeVo.getItemId());
        if (ResultCodeEnum.FAIL.getCode().equals(vipServiceConfigResult.getCode())) {
            throw new GuiguException(ResultCodeEnum.REMOTE_ERROR);
        }
        VipServiceConfig vipServiceConfig = vipServiceConfigResult.getData();
        Assert.notNull(vipServiceConfig, "VIP套餐：{}不存在", tradeVo.getItemId());
        // 订单中VIP会员价格 原价=减免价+订单价
        BigDecimal originalAmount = vipServiceConfig.getPrice();
        BigDecimal orderAmount = vipServiceConfig.getDiscountPrice();
        BigDecimal derateAmount = originalAmount.subtract(orderAmount);
        // 封装订单中商品明细列表
        List<OrderDetailVo> orderDetailVoList = new ArrayList<>();
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setItemId(tradeVo.getItemId());
        orderDetailVo.setItemName(vipServiceConfig.getName());
        orderDetailVo.setItemUrl(vipServiceConfig.getImageUrl());
        orderDetailVo.setItemPrice(originalAmount);
        orderDetailVoList.add(orderDetailVo);

        // 封装订单中优惠明细列表
        List<OrderDerateVo> orderDerateVoList = new ArrayList<>();
        if (derateAmount.compareTo(BigDecimal.ZERO) > 0) {
            OrderDerateVo orderDerateVo = new OrderDerateVo();
            orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_VIP_SERVICE_DISCOUNT);
            orderDerateVo.setDerateAmount(derateAmount);
            orderDerateVo.setRemarks("VIP限时优惠：" + derateAmount);
            orderDerateVoList.add(orderDerateVo);
        }

        this.setParam(orderInfoVo, originalAmount, orderAmount, derateAmount, orderDetailVoList, orderDerateVoList);

        return orderInfoVo;
    }
}
